﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{


    public partial class TICC_ABAX 
    { 

        public TICC_ABAX()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_ABAX();
            MESSAGES += "MULOAD;";
            this.DataContext = MV;
            InitializeComponent(); 
        }
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "MULOAD":
                    MULOAD(msg);
                    break;

            }
        }
        private void MULOAD(MessageInfo msg)
        {
            if (!FTMULOAD)
                return;
            if (msg.LFD.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            bool IsExist = false;
            DataRow dwData = TBMain.NewRow();
            ((VICC_ABAX)MV).SetUpNewRow(ref dwData);
            dwData["ABAAUFLFD"] = "000000";
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVKAUFTYP"] = msg.AUFTYP;
            dwData["ABAVKAUFKRS"] = msg.KRS;
            dwData["ABAVKAUFLFD"] = msg.LFD;
            dwData["ABAVKAUFPOS"] = msg.POS;
            dwData["ABAVKUPOS"] = Helpers.Helper.Right("00" + msg.P1, 2);
            SResult = ((VICC_ABAX)MV).QueryICC_ABAX(dwData, 10);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                IsExist = true;
                dwData.ItemArray = row.ItemArray;
            }
            dwData["ABAWAKZ"] = "F";
            dwData["ABAART"] = msg.ART;
            dwData["ABAVAR"] = msg.VAR;
            dwData["ABAFARBE"] = msg.FARBE;
            dwData["ABABESCHICH"] = msg.BESCHICH;
            dwData["ABAVKART"] = msg.DESS;
            dwData["ABAVKFARBE"] = msg.COLORIT;
            dwData["ABARESMG"] = System.Convert.ToDouble(msg.QTY) * 1000;
            if (!IsExist)
            {
                dwData["ABAID"] = INTEXDBC.GetSeq();
                dwData["ABAUSER"] = app.User;
                dwData["ABADATE"] = vm.date;
                ((VICC_ABAX)MV).InsertICC_ABAX(dwData, 1);
            }
            Insert(dwData);
        }

        public void Insert(DataRow _dwMU)
        {
            TBMain.Rows.InsertAt(_dwMU, 0);
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void Save()
        {
            if (dwMU == null)
                return;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = vm.date;
            ((VICC_ABAX)MV).UpdateICC_ABAX(dwMU, 0);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
            {
                return;
            }
            filter = "LSLIEFNR='" + dwMU["LSLIEFNR"].ToString() + "' AND ABAAUFLFD='000000'";
            rows = TBMain.Select(filter);
            if (rows.Length == 0)
                return;
            int pos = 1;
            LFD = VSDDAT.SD22.GETNEXT("0022", KRS, "EKAUFTR");
            MessageInfo msg = new MessageInfo();
            foreach (DataRow row in rows)
            {
                row["ABAAUFTYP"] = "P";
                row["ABAAUFKRS"] = KRS;
                row["ABAAUFLFD"] = LFD;
                row["ABAAUFPOS"] = Helpers.Helper.Right("000" + pos, 3); ;
                row["ABAAPOS"] = "0002";
                row["ABAQTYRESERVE"] = row["ABARESMG"];
                pos++;
                ((VICC_ABAX)MV).UpdateICC_ABAX(dwMU, 0);
            }
            DataTable tb = rows.CopyToDataTable();
            DataSet Set = new DataSet();
            Set.Merge(tb);
             
            
            PM["KRS"] = dwMU["ABAAUFKRS"].ToString();
            PM["LFD"] = dwMU["ABAAUFLFD"].ToString();
            PM["LIEFSCH"] = dwMU["LSLIEFNR"].ToString();
            PM["WAEHRSL"] = dwMU["F01DES"].ToString();
            PM["Set"] = Set;
            VEK.EKAUTO(PM);
            //VEKP.EKAUTO(PM);
            VKP.MUREV(PM);         
            base.NewCommand(sender, e);
        }
       VEKKOPF _VEK;
       public VEKKOPF VEK
        {
            get
            {
                if (_VEK == null) _VEK = new VEKKOPF();
                return _VEK;
            }
        }
       VEKPOS _VEKP;
       public VEKPOS VEKP
       {
           get
           {
               if (_VEKP == null) _VEKP = new VEKPOS();
               return _VEKP;
           }
       }
        VVKPOS _VKP;
        public VVKPOS VKP
        {
            get
            {
                if (_VKP == null) _VKP = new VVKPOS();
                return _VKP;
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            Save();
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
                return;
            ((VICC_ABAX)MV).DeleteICC_ABAX(dwMU, 1);
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
        VWASTAMM WS;

        private void CheckLiefDat(object sender, RoutedEventArgs e)
        {
            if (WS == null)
            {
                WS = new VWASTAMM();
            }
            foreach (DataRow row in TBMain.Rows)
            {
                dwData = WS.TBMain.NewRow();
                dwData["WAMANDANT"] = app.Mandant;
                dwData["WAART"] = row["ABAART"];
                dwData["WAVAR"] = row["ABAVAR"];
                SResult = WS.QueryWASTAMM(dwData, 11);
                foreach (DataRow ws in SResult.Tables[0].Rows)
                {
                    row["LSLIEFNR"] = ws["WALIEFNR"];
                    row["PREK"] = ws["WABIND"].ToString().Substring(0, 8);//单价
                    row["F01DES"] = ws["WABIND"].ToString().Substring(8, 3);//币种
                    row["UM"] = ws["WABIND"].ToString().Substring(12, 3); //单位 
                }
            }
        }
    }
}
